import 'package:shared_preferences/shared_preferences.dart';
import 'package:xingsu/global/constant.dart';

class UserPreferences {
  static UserPreferences? _singleton;
  static SharedPreferences? _prefs;

  static Future<UserPreferences> getInstance() async {
    if (_singleton == null) {
      if (_singleton == null) {
        var singleton = UserPreferences._();
        await singleton._init();
        _singleton = singleton;
      }
    }
    return _singleton!;
  }

  UserPreferences._();

  Future _init() async {
    _prefs = await SharedPreferences.getInstance();
  }

  static setThemeMode(int themeMode) async {
    if (_prefs == null) return;
    await _prefs!.setInt(Constant.theme, themeMode);
  }

  // 0 跟谁系统  1 白天 2 暗黑
  static int getThemeMode() {
    if (_prefs == null) return 0;
    int isDay = _prefs!.getInt(Constant.theme) ?? 0;
    return isDay;
  }

  static setIsLogin(bool isLogin) async {
    if (_prefs == null) return;
    await _prefs!.setBool(Constant.isLogin, isLogin);
  }

  static bool getIsLogin() {
    if (_prefs == null) return false;
    bool isLogin = _prefs!.getBool(Constant.isLogin) ?? false;
    return isLogin;
  }

  static setAgreeAgreement(bool agreeAgreement) async {
    if (_prefs == null) return;
    await _prefs!.setBool(Constant.agreeAgreement, agreeAgreement);
  }

  static bool getAgreeAgreement() {
    if (_prefs == null) return false;
    bool agreeAgreement = _prefs!.getBool(Constant.agreeAgreement) ?? false;
    return agreeAgreement;
  }

  static setShowPublishGuide(bool showPublishGuide) async {
    if (_prefs == null) return;
    await _prefs!.setBool(Constant.showPublishGuide, showPublishGuide);
  }

  static bool getShowPublishGuide() {
    if (_prefs == null) return false;
    bool showPublishGuide = _prefs!.getBool(Constant.showPublishGuide) ?? false;
    return showPublishGuide;
  }

  static setSearchHistory(List<String> history) async {
    if (_prefs == null) return;
    await _prefs!.setStringList(Constant.searchHistory, history);
  }

  static List<String> getSearchHistory() {
    if (_prefs == null) return [];
    List<String> historys = _prefs!.getStringList(Constant.searchHistory) ?? [];
    return historys;
  }
}
